package com.tqjc.common;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson2.JSON;
import com.qy.led.SendBuffer;
import com.qy.led.model.Material;
import com.tqjc.area.entity.Roller;
import com.tqjc.area.entity.SteelPlate;
import com.tqjc.area.service.RollerService;
import com.tqjc.area.service.SteelPlateService;
import com.tqjc.execute.cache.TaskStatus;
import com.tqjc.execute.task.entity.OcrResult;
import com.tqjc.execute.task.entity.Task;
import com.tqjc.execute.task.service.TaskService;
import com.tqjc.execute.utils.HttpPLCUtils;
import com.tqjc.execute.utils.StaticMethod;
import com.tqjc.mes.entity.SteelInfo;
import com.tqjc.mes.entity.Test;
import com.tqjc.mes.service.SteelInfoService;
import com.tqjc.mes.service.TestService;
import com.tqjc.system.core.redis.TaskRedisApi;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @projectName: BaotouSteel
 * @className: FirstRun
 * @author: Patienyon
 * @description: 初始化方法
 * @date: 2023/6/20 16:54
 */

@Component
@Slf4j
public class FirstRun implements ApplicationRunner {

    @Resource
    SteelInfoService steelInfoService;

    @Resource
    TestService testService;

    @Resource
    TaskService taskService;

    @Resource
    TaskStatus taskStatus;

    @Resource
    RollerService rollerService;

    @Resource
    TaskRedisApi taskRedisApi;

    @Resource
    SteelPlateService steelPlateService;

    @Value("${server_host}")
    private String serverHost;

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public void run(ApplicationArguments args) throws Exception {
       /* Roller roller= rollerService.getById(5);
        String[] lockArr=roller.getLockSignal().split("-");
        HttpPLCUtils.plcWritebcPlus(new RestTemplate(),"http://192.168.12.1","11C北",lockArr[1],HttpPLCUtils.dataTypeList.get("bool"),"FALSE",lockArr[0],true);
*/
/*        while (true){
            String crane = "2#横移";   // "2#横移", "3#横移", "4#横移", "5#横移", "CD0", "11C南", "11C北"
            String val=HttpPLCUtils.plcReadbc(restTemplate,serverHost,crane,"大车位置",false);
            log.info("11111111111111111111111111111111111111111======="+val);
        }*/

        //taskStatus.setNo2ImgSrcByCraneType("2#横移","20240425/202404251535175181.bmp");

        /*String lastNo="";
        while (true) {
            Roller roller = rollerService.getById(1);   //4:5#;3:4#;  5:dj2; 6:dj3;2:3#;1:2#
            String crane = "2#横移";   // "2#横移", "3#横移", "4#横移", "5#横移", "CD0", "11C南", "11C北"
            String isSelected="";

            //String[] lockArr2 = roller.getLockSignal().split("-");
            //写一级辊道锁定信号
            //HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,crane,lockArr2[1],HttpPLCUtils.dataTypeList.get("bool"),"FALSE",lockArr2[0],true);
            if(!"3#横移".equals(crane)&&!"2#横移".equals(crane)){
                //判断一级辊道上PLC选择哪台车下料
                isSelected = HttpPLCUtils.plcReadbc(restTemplate, serverHost, crane, roller.getSelectSignal(), true);
                //isSelected="true";
                if(!"true".equalsIgnoreCase(isSelected)){
                    log.info("一级辊道选择信号为false");
                    //continue;
                    //return null;
                }
            }
            //读取一级辊道允许信号
            String isAllow = HttpPLCUtils.plcReadbc(restTemplate, serverHost, crane, roller.getAllowSignal(), true);
            //String isAllow="true";
            if (!"true".equalsIgnoreCase(isAllow)) {
                log.info("一级辊道允许信号为false");
                //continue;
                //return null;
            }
            String[] lockArr = roller.getLockSignal().split("-");
            //写一级辊道锁定信号
            HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,crane,lockArr[1],HttpPLCUtils.dataTypeList.get("bool"),"FALSE",lockArr[0],true);
            //获取一级辊道钢板号
            String steelNo = HttpPLCUtils.plcReadbc(restTemplate, serverHost, crane, roller.getSteelSignal(), true); //获取钢板号
            //System.out.println("原始钢板号：" + steelNo);
            if ("0".equals(steelNo)||steelNo.length()<7||steelNo.contains("-")) {
                //System.out.println("无效钢板号" + steelNo);
                log.info("无效钢板号:" + steelNo);
                log.info("---------------------------------------------------------------------------------------------------------");
                continue;
            }
            if(StringUtils.isNotEmpty(steelNo)&&steelNo.length()==7){
                steelNo="0"+steelNo;
            }
            if(steelNo.equals(lastNo)){
                continue;
            }
            log.info("原始钢板号：" + steelNo);
            lastNo=steelNo;

            steelNo = StaticMethod.updateSteelNo(crane, steelNo);
            List<Test> steels = testService.selectByMatNo(steelNo);
            //List<SteelInfo> steels= steelInfoService.selectByMatNo(steelNo);
            //String length = HttpPLCUtils.plcReadbc(restTemplate, serverHost, crane, "dj2dj3钢板长度", true);
            //log.info("dj2dj3钢板长度:"+length);
            if (steels != null&&steels.size()==1) {
                //System.out.println("MES数据库中有此钢板号" + steelNo + "信息，为：" + steels.get(0).getMatNo());
                log.info("MES数据库中有此钢板号" + steelNo + "信息，为：" + steels.get(0).getMatNo()+",长="+steels.get(0).getMatActLen()+"，宽="+steels.get(0).getMatActWidth()+"，厚="+steels.get(0).getMatActThick()+"，重="+steels.get(0).getMatActWt());
            } else if(steels != null&&steels.size()==0){
                //System.out.println("MES数据库中无此钢板号" + steelNo + "信息");
                log.info("MES数据库中无此钢板号" + steelNo + "信息");
            }else if(steels != null&&steels.size()>1){
                log.info("MES数据库中有多条此钢板号" + steelNo + "信息");
            }
            List<Test> steels2 = testService.selectByMatNo(lastNo);
            //List<SteelInfo> steels2= steelInfoService.selectByMatNo(lastNo);
            if (steels2 != null&&steels2.size()==1) {
                //System.out.println("MES数据库中有此钢板号" + steelNo + "信息，为：" + steels.get(0).getMatNo());
                log.info("0000000MES数据库中有此钢板号" + lastNo + "信息，为：" + steels2.get(0).getMatNo());
            } else if(steels2 != null&&steels2.size()==0){
                //System.out.println("MES数据库中无此钢板号" + steelNo + "信息");
                log.info("0000000MES数据库中无此钢板号" + lastNo + "信息");
            }else if(steels2 != null&&steels2.size()>1){
                log.info("0000000MES数据库中有多条此钢板号" + lastNo + "信息");
            }
            log.info("-----------------------------------------------------------------------------------------------------------------------------");
        }*/

        //List<SteelInfo> list=steelInfoService.selectAll();
        //testService.insertAll(list);
        //log.info("共转移钢板信息数据："+list.size()+"条");
        //com.tqjc.mes.entity.SteelInfo data= steelInfoService.selectByMatNo("B24308142000");
        //log.info("oracle数据库钢板信息："+data.getMatNo()+","+data.getMatActThick()+","+data.getMatActLen()+","+data.getMatActWidth()+","+data.getMatActWt());
        //initQuartzTask();
        //HttpPLCUtils.plcWritebc(new RestTemplate(),"http://192.168.12.1",HttpPLCUtils.macSnMap.get("5#横移"),"DB20.6.1",HttpPLCUtils.dataTypeList.get("bool"),"TRUE");
        //HttpPLCUtils.plcWritebc(new RestTemplate(),"http://192.168.12.1",HttpPLCUtils.macSnMap.get("5#横移"),"DB20.2",HttpPLCUtils.dataTypeList.get("int16"),"1");
        //String val=HttpPLCUtils.plcReadbc(new RestTemplate(),"http://192.168.12.1",HttpPLCUtils.snMap.get("5#横移"),"下料A完成");
        //HttpPLCUtils.plcWritebcPlus(new RestTemplate(),"http://192.168.12.1","5#横移","DB40.10.1",HttpPLCUtils.dataTypeList.get("bool"),"TRUE","5#8组辊道锁紧信号",true);
        //String val2=HttpPLCUtils.plcReadbc(new RestTemplate(),"http://192.168.12.1","5#横移","5#8组辊道锁紧信号",true);
        //System.out.println(val2);
    }
    private void send2(String ip){
            if (SendBuffer.SendInternalText_Net("本地手动", "192.168.12."+ip, 1,
                    104,
                    20, 1, 1, 9, 1, 1, 1, 1, 1, 3, false, 0) == 0) {
                System.out.println("发送内码文字成功！");
            }
            if (SendBuffer.SendInternalText_Net("大车停止", "192.168.12."+ip, 1,
                    104,
                    20, 2, 1, 9, 1, 1, 1, 1, 1, 3, false, 0) == 0) {
                System.out.println("发送内码文字成功！");
            }
            if (SendBuffer.SendInternalText_Net("起升停止", "192.168.12."+ip, 1,
                    104,
                    20, 3, 1, 9, 1, 1, 1, 1, 1, 3, false, 0) == 0) {
                System.out.println("发送内码文字成功！");
            }
            if (SendBuffer.SendInternalText_Net("湖南天桥嘉成智能科技有限公司", "192.168.12."+ip, 1,
                    160,
                    20, 4, 1, 1, 4, 1, 2, 1, 0, 3, false, 0) == 0) {
                System.out.println("发送内码文字成功！");
            }
    }


    private void sendMoBan2(String ip){
        if (SendBuffer.AddShowPage("00:00", "23:59", 0) == 0) {
            System.out.println("添加显示页成功！");
            if (SendBuffer.AddArea(48, 0, 104, 20) == 0) {
                System.out.println("添加区域1成功！");
                if (SendBuffer.AddTemplate_InternalText("本地自动11", 1, 1, 9, 1, 1, 2,
                        1, 1, false) == 0) {
                    System.out.println("添加内码文字素材成功！");
                }
            }
            if (SendBuffer.AddArea(48, 20, 104, 20) == 0) {
                System.out.println("添加区域2成功！");
                if (SendBuffer.AddTemplate_InternalText("大车运行11", 2, 1, 9, 1, 1, 2,
                        1, 1, false) == 0) {
                    System.out.println("添加内码文字素材成功！");
                }
            }
            if (SendBuffer.AddArea(48, 40, 104, 20) == 0) {
                System.out.println("添加区域1成功！");
                if (SendBuffer.AddTemplate_InternalText("起升运行11", 3, 1, 9, 1, 1, 2,
                        1, 1, false) == 0) {
                    System.out.println("添加内码文字素材成功！");
                }
            }
            if (SendBuffer.AddArea(0, 60, 160, 20) == 0) {
                System.out.println("添加区域1成功！");
                if (SendBuffer.AddTemplate_InternalText("湖南天桥嘉成智能科技有限公司", 4, 1, 9, 1, 1, 2,
                        1, 0, false) == 0) {
                    System.out.println("添加内码文字素材成功！");
                }
            }
            if (SendBuffer.SendTemplateData_Net("192.168.12."+ip, 1) == 0) {
                System.out.println("发送模板成功！");
                /*if (SendBuffer.SendInternalText_Net("本地手动", "192.168.12."+ip, 1,
                        104,
                        16, 1, 1, 9, 1, 1, 1, 1, 0, 3, false, 0) == 0) {
                    System.out.println("发送内码文字成功！");
                }
                if (SendBuffer.SendInternalText_Net("大车停止", "192.168.12."+ip, 1,
                        104,
                        16, 2, 1, 9, 1, 1, 1, 1, 0, 3, false, 0) == 0) {
                    System.out.println("发送内码文字成功！");
                }
                if (SendBuffer.SendInternalText_Net("起升停止", "192.168.12."+ip, 1,
                        104,
                        16, 3, 1, 9, 1, 1, 1, 1, 0, 3, false, 0) == 0) {
                    System.out.println("发送内码文字成功！");
                }
                if (SendBuffer.SendInternalText_Net("湖南天桥", "192.168.12."+ip, 1,
                        104,
                        16, 4, 1, 9, 1, 1, 1, 1, 0, 3, false, 0) == 0) {
                    System.out.println("发送内码文字成功！");
                }*/
            }
        }
    }
    /**
     * @param :
     * @return void
     * @author Patienyon
     * @description 初始化所有的定时任务
     * @date 2023/6/20 16:56
     */
    private void initQuartzTask(){
        //HttpPLCUtils.plcWritebc(new RestTemplate(),"http://192.168.12.10","2#PLC_Mac_Sn","DB3.84",4,"1");
        while (true){
            /*String val2= HttpPLCUtils.plcReadbc(new RestTemplate(),"http://192.168.12.1","BakS_9261cd82f7ad432c994484bd528c5eb6","DJ2辊道钢板就位");
            log.info("dj2辊道钢板就位信号："+val2);
            String val3= HttpPLCUtils.plcReadbc(new RestTemplate(),"http://192.168.12.1","BakS_9261cd82f7ad432c994484bd528c5eb6","DJ3辊道钢板就位");
            log.info("dj3辊道钢板就位信号："+val3);*/

            //String val4= HttpPLCUtils.plcReadbc(new RestTemplate(),"http://192.168.12.1","BakS_bd647473814245798eb129cc15bad4f6","辊道坐标X");
            //log.info("辊道X坐标="+val4);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

        //String val2= HttpUtils.plcReadbc(new RestTemplate(),"http://192.168.12.10","BakS_c13cfe36e2f04c8a9203d69efa81815b","接受任务X");
    }

    public void test3(){
        SteelPlate steelPlate=new SteelPlate();
        steelPlate.setWidth(100);
        String str= JSON.toJSONString(steelPlate);
        taskRedisApi.set("123",str);
        String str2= (String) taskRedisApi.get("123");
        SteelPlate steelPlate2= JSON.parseObject(str2,SteelPlate.class);
        System.out.println(steelPlate2.getWidth());
    }

}
